#include "ringqueue.hpp"
#include "Thread.hpp"
#include<string>
#include<vector>
#include<unistd.h>
#include<functional>
#include<pthread.h>

using namespace Thread_Module;

void* consumer(RingQueue<int> &rq)
{
    while(true)
    {
        int data;
        rq.Pop(&data);
        std::cout<<"消费一个数据:"<<data<<std::endl;
        sleep(1);
    }  
}

void* productor(RingQueue<int> &rq)
{   
    int a = 1;
    while(true)
    {
        rq.Enqueue(a);
        std::cout<<"生产一个数据 :"<<a<<std::endl;
        a++;
    }
}

void Comm(std::vector<Thread<RingQueue<int>>> *threads,int num,RingQueue<int> &rq,func_t<RingQueue<int>> func)
{
    for(int i=0;i<num;i++)
    {
        std::string name = "thread-"+std::to_string(i+1);
        threads->emplace_back(func,rq,name);
    }
}

void ProductorStart(std::vector<Thread<RingQueue<int>>> *threads,int num,RingQueue<int> &rq)
{
    Comm(threads,num,rq,productor);
}

void ConsumerStart(std::vector<Thread<RingQueue<int>>> *threads,int num,RingQueue<int> &rq)
{
    Comm(threads,num,rq,consumer);
}

void StartAll(std::vector<Thread<RingQueue<int>>> &threads)
{
    for(auto &thread:threads)
    {
        std::cout<<"Start:"<<thread.threadname()<<std::endl;
        thread.Start();
    }
}

void WaitAllThread(std::vector<Thread<RingQueue<int>>> &threads)
{
    for(auto &thread:threads)
    {
        thread.Join();
    }
}


int main()
{
    RingQueue<int> *rq = new RingQueue<int>(10);
    std::vector<Thread<RingQueue<int>>> threads;

    ProductorStart(&threads,1,*rq);
    ConsumerStart(&threads,2,*rq);
    StartAll(threads);
    WaitAllThread(threads);

    return 0;
}